package com.jztd.servone.invoice.util.invoice;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;

public class OpenApi
{
    private final String platformCode;
    private final String prefix;
    private final String password;
    private final String privateKey;

    public OpenApi(String password, String platformCode, String prefix, String privateKey)
    {
        this.password = password;
        this.platformCode = platformCode;
        this.prefix = prefix;
        this.privateKey = privateKey;
    }

    public String buildRequest(String content)
    {
        return OpenAPIUtil.buildRequestData(this.platformCode, this.prefix, content, this.password, this.privateKey);
    }

    public String disposeResponse(String responseStr, String ptKey)
    {
        return OpenAPIUtil.disposeResponse(responseStr, ptKey, this.password);
    }

    private static class OpenAPIUtil
    {
        private static final String VERSION = "1.0";
        private static final String FORMAT = "JSON";
        private static final String SIGNTYPE = "RSA";
        private static final String PLATFORMCODE_FIELD = "platformCode";
        private static final String SIGNTYPE_FIELD = "signType";
        private static final String SIGN_FIELD = "sign";
        private static final String FORMAT_FIELD = "format";
        private static final String TIMESTAMP_FIELD = "timestamp";
        private static final String VERSION_FIELD = "version";
        private static final String SERIALNO_FIELD = "serialNo";
        private static final String CONTENT_FIELD = "content";

        public static String buildRequestData(String platformCode, String prefix, String content, String password, String privateKey)
        {
            Map map = new HashMap();

            String reqContent = SecurityUtil.encrypt3DES(password, content);
            map.put("platformCode", platformCode);
            map.put("signType", "RSA");
            map.put("format", "JSON");
            map.put("version", "1.0");
            map.put("content", reqContent);
            map.put("timestamp", DateUtil.date().getTime());
            map.put("serialNo", UUIDUtil.getSerialNo(prefix));
            map.put("sign", RSAUtil.sign(RSAUtil.getSignatureContent(map), privateKey));
            return JSONUtil.toJsonStr(map);
        }

        public static String disposeResponse(String jsonStr, String publicKey, String password) {
//            JsonObject jsonObject = new JsonParser().parse(jsonStr).getAsJsonObject();
//            Map map = JsonUtil.json2Map(jsonStr);
//            String sign = (String)map.remove("sign");
//            if (RSAUtil.verify(RSAUtil.getSignatureContent(map), sign, publicKey)) {
//                String plainContent = SecurityUtil.decrypt3DES(password, (String)map.get("content"));
//                jsonObject.add("content", new JsonParser().parse(plainContent));
//            } else {
//                throw new IllegalStateException("验签失败");
//            }
//            return jsonObject.toString();
            return null;
        }
    }
}
